package com.git.smp.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.ApplyFieldMapper;
import com.git.smp.dao.SmpFieldMapper;
import com.git.smp.dao.SmpFileMapper;
import com.git.smp.dao.SmpServiceHisMapper;
import com.git.smp.dao.SmpServiceMapper;
import com.git.smp.dao.smpFieldHisMapper;
import com.git.smp.entity.ApplyField;
import com.git.smp.entity.RequireApply;
import com.git.smp.entity.SmpField;
import com.git.smp.entity.SmpFieldKey;
import com.git.smp.entity.SmpService;
import com.git.smp.entity.SmpServiceHis;
import com.git.smp.entity.smpFieldHis;
import com.git.smp.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("/field")
public class SmpFieldController {
	
	Logger logger = LoggerFactory.getLogger(SmpFieldController.class);
	
	@Autowired
	SmpServiceMapper smpServiceMapper;
	
	@Autowired
	SmpServiceHisMapper smpServiceHisMapper;

	@Autowired
	SmpFieldMapper smpFiledMapper;
	
	@Autowired
	ApplyFieldMapper applyFiledMapper;
	
	@Autowired
	smpFieldHisMapper fieldHisMapper;
	
	@RequestMapping("/getFieldByServer")
	public ResponseMessage<PageInfo<SmpField>> getFieldByServer(@RequestBody SmpField smpFile) {
		
		ResponseMessage<PageInfo<SmpField>> responseMessage = new ResponseMessage<PageInfo<SmpField>>();
		
		List<SmpField> smpFileList = smpFiledMapper.getFieldByServer(smpFile);
		
		PageInfo<SmpField> pageInfo = new PageInfo<SmpField>(smpFileList);

		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
	
	
	
	@RequestMapping("/getFieldBySmpAndApply")
	public ResponseMessage<Map> getFieldBySmp(@RequestBody ApplyField applyField) {
		
		ResponseMessage<Map> responseMessage = new ResponseMessage<Map>();
		
		SmpField smpField0  = new SmpField();
		smpField0.setTransCode(applyField.getTransCode());
		smpField0.setSystemNo(applyField.getSystemNo());
		
		List<SmpField> smpFieldList = smpFiledMapper.getFieldByServer(smpField0);
		
		List<ApplyField> applyFieldList = applyFiledMapper.selectByApplyIdAndTransCode(applyField);
		
		
		for (SmpField smpField : smpFieldList) {
			String versionType = "04"; //删除
			for(ApplyField applyField2: applyFieldList) {
				if(	
						StringUtil.equals(smpField.getFieldCode(),applyField2.getFieldCode()) &&
					    StringUtil.equals(smpField.getParentType(),applyField2.getParentType()) &&
					    StringUtil.equals(smpField.getStruct(),applyField2.getStruct()) &&
					    StringUtil.equals(smpField.getInOut(),applyField2.getInOut())
				   ) { //字段相等
					
					if(
					/*
					 * StringUtil.equals(smpField.getOrderNumber(),applyField2.getOrderNumber()) &&
					 */						    StringUtil.equals(smpField.getFieldName(),applyField2.getFieldName()) &&
						    StringUtil.equals(smpField.getNodeType(),applyField2.getNodeType()) &&
						    StringUtil.equals(smpField.getFieldType(),applyField2.getFieldType()) &&
						    StringUtil.equals(smpField.getFieldLength(),applyField2.getFieldLength()) &&
						    StringUtil.equals(smpField.getFieldRequire(),applyField2.getFieldRequire()) &&
						    StringUtil.equals(smpField.getFieldEdsc(),applyField2.getFieldEdsc())
								) {
							versionType = "01"; //正常
						}else {
							versionType = "02"; //修改
						}
					
					break;
				}
			}
			smpField.setVersionType(versionType);
			
		}
		
		for (ApplyField applyField2: applyFieldList) {
			String versionType = "03"; //删除
			for(SmpField smpField : smpFieldList) {
				if(
						StringUtil.equals(smpField.getFieldCode(),applyField2.getFieldCode()) &&
					    StringUtil.equals(smpField.getParentType(),applyField2.getParentType()) &&
					    StringUtil.equals(smpField.getStruct(),applyField2.getStruct()) &&
					    StringUtil.equals(smpField.getInOut(),applyField2.getInOut())
				   ) { //字段相等
					if(
					/*
					 * StringUtil.equals(smpField.getOrderNumber(),applyField2.getOrderNumber()) &&
					 */		StringUtil.equals(smpField.getFieldName(),applyField2.getFieldName()) &&
						    StringUtil.equals(smpField.getNodeType(),applyField2.getNodeType()) &&
						    StringUtil.equals(smpField.getFieldType(),applyField2.getFieldType()) &&
						    StringUtil.equals(smpField.getFieldLength(),applyField2.getFieldLength()) &&
						    StringUtil.equals(smpField.getFieldRequire(),applyField2.getFieldRequire()) &&
						    StringUtil.equals(smpField.getFieldEdsc(),applyField2.getFieldEdsc())
								) {
							versionType = "01"; //正常
						}else {
							versionType = "02"; //修改
						}
					break;
				}
			}
			applyField2.setVersionType(versionType);
		}
		
		Map map = new HashMap();
		map.put("applyFieldList", applyFieldList);
		map.put("smpFieldList", smpFieldList);
		
		responseMessage.setExtend(map);
		
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
	

	@RequestMapping("/getHisFieldByServer")
	public ResponseMessage<PageInfo<smpFieldHis>> getHisFieldByServer(@RequestBody smpFieldHis fieldHis) {
		
		ResponseMessage<PageInfo<smpFieldHis>> responseMessage = new ResponseMessage<PageInfo<smpFieldHis>>();
		
		List<smpFieldHis> smpFieldHisList = fieldHisMapper.getHisFieldByServer(fieldHis);
		
		PageInfo<smpFieldHis> pageInfo = new PageInfo<smpFieldHis>(smpFieldHisList);

		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
	
	
	
	/**
	 * 历史版本字段比较
	 * @param id
	 * @return
	 */
	@RequestMapping("/getFieldBySmpAndHis")
	public ResponseMessage<Map> getFieldBySmpAndHis(@RequestParam String id) {
		
		ResponseMessage<Map> responseMessage = new ResponseMessage<Map>();
		SmpServiceHis smpServiceHis = smpServiceHisMapper.selectByPrimaryKey(id);
		
		SmpService smpService = new SmpService();
		smpService.setBusCode(smpServiceHis.getBusCode());
		smpService.setSystemNo(smpServiceHis.getSystemNo());

		smpService = smpServiceMapper.getServiceInfo(smpService);

		
		SmpField smpField0  = new SmpField();
		smpField0.setTransCode(smpService.getTransCode());
		smpField0.setSystemNo(smpService.getSystemNo());
		
		List<SmpField> smpFieldList = smpFiledMapper.getFieldByServer(smpField0);
		
		
		smpFieldHis fieldHis = new smpFieldHis();
		fieldHis.setServiceHisId(id);
		List<smpFieldHis> fieldHisList = fieldHisMapper.getHisFieldByServer(fieldHis);
		
		
		for (SmpField smpField : smpFieldList) {
			String versionType = "03"; //删除
			for(smpFieldHis smpFieldHis2: fieldHisList) {
				if(	
						StringUtil.equals(smpField.getFieldCode(),smpFieldHis2.getFieldCode()) &&
					    StringUtil.equals(smpField.getParentType(),smpFieldHis2.getParentType()) &&
					    StringUtil.equals(smpField.getStruct(),smpFieldHis2.getStruct()) &&
					    StringUtil.equals(smpField.getInOut(),smpFieldHis2.getInOut())
				   ) { //字段相等
					
					if(
					/*
					 * StringUtil.equals(smpField.getOrderNumber(),applyField2.getOrderNumber()) &&
					 */		StringUtil.equals(smpField.getFieldName(),smpFieldHis2.getFieldName()) &&
						    StringUtil.equals(smpField.getNodeType(),smpFieldHis2.getNodeType()) &&
						    StringUtil.equals(smpField.getFieldType(),smpFieldHis2.getFieldType()) &&
						    StringUtil.equals(smpField.getFieldLength(),smpFieldHis2.getFieldLength()) &&
						    StringUtil.equals(smpField.getFieldRequire(),smpFieldHis2.getFieldRequire()) &&
						    StringUtil.equals(smpField.getFieldEdsc(),smpFieldHis2.getFieldEdsc())
								) {
							versionType = "01"; //正常
						}else {
							versionType = "02"; //修改
						}
					
					break;
				}
			}
			smpField.setVersionType(versionType);
			
		}
		
		for (smpFieldHis smpFieldHis2: fieldHisList) {
			String versionType = "04"; //新增
			for(SmpField smpField : smpFieldList) {
				if(
						StringUtil.equals(smpField.getFieldCode(),smpFieldHis2.getFieldCode()) &&
					    StringUtil.equals(smpField.getParentType(),smpFieldHis2.getParentType()) &&
					    StringUtil.equals(smpField.getStruct(),smpFieldHis2.getStruct()) &&
					    StringUtil.equals(smpField.getInOut(),smpFieldHis2.getInOut())
				   ) { //字段相等
					if(
					/*
					 * StringUtil.equals(smpField.getOrderNumber(),applyField2.getOrderNumber()) &&
					 */		StringUtil.equals(smpField.getFieldName(),smpFieldHis2.getFieldName()) &&
						    StringUtil.equals(smpField.getNodeType(),smpFieldHis2.getNodeType()) &&
						    StringUtil.equals(smpField.getFieldType(),smpFieldHis2.getFieldType()) &&
						    StringUtil.equals(smpField.getFieldLength(),smpFieldHis2.getFieldLength()) &&
						    StringUtil.equals(smpField.getFieldRequire(),smpFieldHis2.getFieldRequire()) &&
						    StringUtil.equals(smpField.getFieldEdsc(),smpFieldHis2.getFieldEdsc())
								) {
							versionType = "01"; //正常
						}else {
							versionType = "02"; //修改
						}
					break;
				}
			}
			smpFieldHis2.setVersionType(versionType);
		}
		
		Map map = new HashMap();
		map.put("fieldHisList", fieldHisList);
		map.put("smpFieldList", smpFieldList);
		
		responseMessage.setExtend(map);
		
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
	

}
